//
//  FabuController.m
//  GuShiNews
//
//  Created by huachun on 2019/8/22.
//  Copyright © 2019 Jones. All rights reserved.
//

#import "FabuController.h"
#import "AddModel.h"
#import "FabuCell.h"
#import "SensitiveWordTools.h"
#import "AddLunViewModel.h"
#import "OSSTestMacros.h"
#import "AliyunUploadBlock.h"

@interface FabuController ()<AddLunDelegate>

@end

@implementation FabuController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"发帖";
    
    UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc] initWithTitle:@"发布" style:UIBarButtonItemStylePlain target:self action:@selector(finishSelecting)];
    [cancelItem setTintColor:NavItemTitleColor];
    self.navigationItem.rightBarButtonItem = cancelItem;
    
    self.Data_array = [NSMutableArray new];
    NSArray *linlist = @[@"请输入标题",@"请输入正文",@"标签（如NBA）",@"选择照片"];
    [linlist enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        AddModel *model = [AddModel new];
        model.Placeholder = obj;
        model.imgarray = [NSMutableArray new];
        if (idx==0) {
            model.Height = 55;
            model.type = 1;
        }else if (idx==1){
            model.Height = 150;
            model.type = 0;
        }else if (idx==2){
            model.Height = 55;
            model.type = 1;
        }else{
            model.type = 2;
            model.Height = (RECRWITCH-60-20)/3 + 60;
        }
        [self.Data_array addObject:model];
    }];
    
    
    [self.view addSubview:self.TableView];
    self.TableView.frame=RECR;
}

- (void)finishSelecting{
    __block NSString *errorstring;
    
    [SVProgressHUD showWithStatus:@"发布中.."];
    
    NSMutableDictionary *dic = [NSMutableDictionary new];
    [dic setValue:[UserModel info].userid forKey:@"user_id"];
    
    [self.Data_array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        AddModel *model = (AddModel *)obj;
        NSString *linstrong = model.Contont;
        
        if (idx==0) {
            if (linstrong.length<5) {
                errorstring = @"标题至少输入5个字节";
                *stop = YES;
            }else{
                [dic setValue:model.Contont forKey:@"tittle"];
            }
        }else if (idx==1) {
            if (linstrong.length<10) {
                errorstring = @"正文至少输入10个字节";
                *stop = YES;
            }else{
                [dic setValue:model.Contont forKey:@"info"];
            }
        }else if (idx==2){
            if (linstrong.length<2) {
                errorstring = @"标签至少输入2个字节";
                *stop = YES;
            }else{
                [dic setValue:model.Contont forKey:@"type"];
            }
        }
        
        if (!errorstring) {
            BOOL hasSensitive = [[SensitiveWordTools sharedInstance] hasSensitiveWord:linstrong];
            
            if (hasSensitive) {
                errorstring = [NSString stringWithFormat:@"含有敏感词汇：%@",[[SensitiveWordTools sharedInstance]filter:linstrong]];
                *stop = YES;
            }
        }
    }];
    
    
    
    
    
    
    if (!errorstring) {
        
        AddModel *imgmodel = [self.Data_array lastObject];
        
        if (imgmodel.imgarray.count>0) {
            [SVProgressHUD showWithStatus:@"上传中.."];
            [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
            __block NSInteger index = 0;
            
            NSMutableArray *lin_img_list = [NSMutableArray new];
            [imgmodel.imgarray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                NSString *url_img = [NSString stringWithFormat:@"000lin/%@%ld.png",[JonesClass generateUuidString],idx];
                [AliyunUploadBlock AliyunUploadimg:[obj objectForKey:@"img"] imgname:url_img finished:^(OSSTask * _Nonnull task, NSString * _Nonnull imgurl) {
                    index ++;
                    if (imgurl) {
                        [lin_img_list addObject:imgurl];
                    }
                    if (index == imgmodel.imgarray.count) {
                        
                        NSString *arrrayString = [lin_img_list componentsJoinedByString:@"&&"];
                        [dic setValue:arrrayString forKey:@"images"];
                        
                        AddLunViewModel *viewmdoel = [[AddLunViewModel alloc] init:dic];
                        viewmdoel.delegate=self;
                    }
                }];
            }];
            
        }else{
            
            [dic setValue:@"" forKey:@"images"];
            
            AddLunViewModel *viewmdoel = [[AddLunViewModel alloc] init:dic];
            viewmdoel.delegate=self;
        }
        
    }else{
        [SVProgressHUD showErrorWithStatus:errorstring];
    }
    
    
    
    
    
    
//
//    if (!errorstring) {
//
//        AddLunViewModel *viewmdoel = [[AddLunViewModel alloc] init:dic];
//        viewmdoel.delegate=self;
//
//    }else{
//        [SVProgressHUD showErrorWithStatus:errorstring];
//    }
}

- (void)AddLunSucces:(int)value{
    if (value==1) {
        [SVProgressHUD showSuccessWithStatus:@"发布成功"];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"FABUCHNEGGONG" object:@{@"done":@"0"}];
        [self goback];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.Data_array.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    AddModel *model = [self.Data_array objectAtIndex:indexPath.row];
    return model.Height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FabuCell *cell = [FabuCell cellForTableView:tableView cellForRowAtIndexPath:indexPath];
    cell.SuperVC = self;
    cell.Model = [self.Data_array objectAtIndex:indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
